package blackitheima3.ClassDemo;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Properties;

public class MyClass {
    public static void main(String[] args) throws IOException, ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException {
        //创建系统类加载器
        ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
        System.out.println(systemClassLoader);

        ClassLoader classLoader = MyClass.class.getClassLoader();
        System.out.println(classLoader);

        //创建平台类加载器
        ClassLoader classLoader1 = classLoader.getParent();
        System.out.println(classLoader1);

        //创建内置类加载器
        ClassLoader classLoader2 = classLoader1.getParent();
        System.out.println(classLoader2);

        //调用系统类加载器 从src类下获得prop.properties 文件内容,返回字节流
        InputStream stream = classLoader.getResourceAsStream("prop.properties");

        //创建Properties对象
        Properties prop=new Properties();
        //将字节流传到prop对象中
        prop.load(stream);
        //调用方法通过键获得值
        String classname = prop.getProperty("Classname");
        String methodClass = prop.getProperty("methodClass");

        //运用反射来实现其他类的成员方法
        //创建Class类
        Class aClass = Class.forName(classname);

        //提取成员方法
        Method declaredMethod = aClass.getDeclaredMethod(methodClass);

        //创建对象
        Object o = aClass.newInstance();

        //调用成员方法
        declaredMethod.invoke(o);

        //提取成员变量
//        Field name = aClass.getDeclaredField("name");
        Field declaredField = aClass.getDeclaredField("name");

        //因为是私有的 所以需要暴力一下
        declaredField.setAccessible(true);
        //给成员变量赋值
        declaredField.set(o,"小雪");
        Object o1 = declaredField.get(o);
        System.out.println(o1);


    }
}
